package com.jpro.module.bpm.service.oa.listener;


import cn.hutool.core.util.StrUtil;
import com.jpro.module.bpm.event.BpmProcessInstanceStatusEvent;
import com.jprocms.module.cms.api.ContentApi;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

import static com.jprocms.module.cms.enums.ApiConstants.PROCESS_KEY_CONTENT_PUBLISH;

/**
 * 内容审核 结果的监听器实现类
 *
 * @author jprocms
 */
@Component
public class BpmContentAuditResultListener implements ApplicationListener<BpmProcessInstanceStatusEvent> {

    @Resource
    private ContentApi contentApi;

    protected String getProcessDefinitionKey() {
        return PROCESS_KEY_CONTENT_PUBLISH;
    }

    protected void onEvent(BpmProcessInstanceStatusEvent event) {
        //内容ID 通过businessKey传递
        contentApi.updateAuditResult(Long.parseLong(event.getBusinessKey()), event.getStatus());
    }

    @Override
    public void onApplicationEvent(BpmProcessInstanceStatusEvent event) {
        /**
         * 内容审核流程模型需要以这个前缀定义流程标识
         */
        if (!StrUtil.startWith(event.getProcessDefinitionKey(), getProcessDefinitionKey())) {
            return;
        }
        onEvent(event);
    }
}
